package com.yz.springbootproducer;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * 描述：
 **/
@Component
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(){
        Message<String> message = MessageBuilder.createMessage("hello", new MessageHeaders(null));
        CorrelationData correlationData = new CorrelationData("zhang123123");
        rabbitTemplate.convertAndSend("directQueue",message, correlationData);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.err.println("correlationData: " + correlationData);
                System.out.println(ack);
            }
        });
    }
}
